package com.luck.pf.util;

import java.util.Random;

import org.apache.commons.lang.RandomStringUtils;

/**
 * 
 * @author TerryZhang
 *
 */
public class RandomUtil
{
    public final static String BASE_STRING = "_0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public final static String BASE_NUMBER = "0123456789";
   

    private static String random(String base, int length)
    { 
        StringBuffer buffer = new StringBuffer(base); 
        StringBuffer sb = new StringBuffer();
        Random r = new Random(); 
        int range = buffer.length();
        for (int i = 0; i < length; i ++)
        { 
            sb.append(buffer.charAt(r.nextInt(range))); 
        } 
        return sb.toString(); 
    }
    /**
     * 取得指定长度的随机字符串
     * @param length
     * @return
     */
    public static String randomNumber(int length){
    	String str = "";
    	while(true){
    		str = random(BASE_NUMBER, length);
    		if(!str.startsWith("0"))
    			break;
    	}
    	return str;
    }
    public static String randomString(int length){
    	return random(BASE_STRING, length);
    }
    public static void main(String[] args) {
    	System.out.println(RandomStringUtils.random(4, BASE_STRING));
	}
}
